package com.meixiang.adapter.service;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.meixiang.R;
import com.meixiang.activity.homes.service.ShowPictureActivity;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.recyclerview.RecyclerAdapter;
import com.meixiang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * author:xiejingwen
 */
public class CosmetologistListAdapter extends RecyclerAdapter<CosmetologistListAdapter.ViewHolder> implements OnItemClick {
    private Context mContext;
    private Fragment fragment;
    private ArrayList<String> urls=new ArrayList<>();
    public CosmetologistListAdapter(Fragment fragment){
        this.fragment=fragment;
        setOnItemClickListener(this);
    }

    public CosmetologistListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void addUrls(List<String> urls){
        this.urls.addAll(urls);
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext=parent.getContext();
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_item_cosmetologist_information, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //设置间隔
        if (position>0){
            holder.itemView.setPadding(DensityUtil.dip2px(mContext,8),0,0,0);
        }
        if(fragment!=null){
            Glide.with(fragment).load(urls.get(position)).placeholder(R.drawable.image_default_gray_bg).error(R.drawable.image_default_gray_bg).into(holder.img);
        }else{
            Glide.with(mContext).load(urls.get(position)).placeholder(R.drawable.image_default_gray_bg).error(R.drawable.image_default_gray_bg).into(holder.img);

        }

    }

    @Override
    public int getItemCount() {
        return urls.size();
    }

    @Override
    public void onItemClick(View view, int position) {
        Intent intent=new Intent(mContext, ShowPictureActivity.class);
        intent.putStringArrayListExtra("urls",urls);
        intent.putExtra("position", position);
        mContext.startActivity(intent);
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.img)
        ImageView img;
        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
